﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Xml.Serialization;

using Common;

namespace Server
{
    class Program
    {
            public static void Main() {
    try {
        IPAddress ipAd = IPAddress.Parse("127.0.0.1");


        /* Initializes the Listener */
            TcpListener myList=new TcpListener(ipAd,8001);
        
        
        /* Start Listeneting at the specified port */
            myList.Start();
        
        
        Console.WriteLine("The server is running at port 8001...");    
        Console.WriteLine("The local End point is  :" + myList.LocalEndpoint );
        Console.WriteLine("Waiting for a connection.....");

        TcpClient tpC = myList.AcceptTcpClient();
        Stream stm = tpC.GetStream();

        var xmlSerializer = new XmlSerializer(typeof(PlainText));
        Object obj = null;

        if (stm.CanRead)
        {
            obj = xmlSerializer.Deserialize(stm);
        }

        Console.WriteLine(obj.ToString());
        if(obj.GetType() == typeof(PlainText))
        {
            Console.WriteLine(obj.ToString());
        }
    
        tpC.Close();
        myList.Stop();
            
    }
    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }    
    }



    }
}
